package cn.micai.base.thread;

/**
 * 描述：
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/7 17:49
 */
public class ThreadExampleMain {

    public static void main(String [] args) {

        System.out.println("Main thread execution starts");

        MyRunnable runnable = new MyRunnable();

        Thread t1 = new Thread(runnable, "Thread 1");
        Thread t2 = new Thread(runnable, "Thread 2");
        Thread t3 = new Thread(runnable, "Thread 3");

        t1.start();
        // lets waits for  t1 to die
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t2.start();
        // lets waits for 1 sec or t2 to die which ever occurs first
        try {
            t2.join(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t3.start();
        // complete all threads before completing main thread
        try {
            t2.join();
            t3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Main thread execution ends");

    }
}
